package com.lagou.service;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.Dao.AccountDao;
import com.lagou.annotion.Autowired;
import com.lagou.annotion.Service;
import com.lagou.annotion.Transactional;
import com.lagou.config.Config;
import com.lagou.factory.BeanFactory;
import com.lagou.mapper.AccountMapper;
import com.lagou.pojo.Account;
import com.lagou.util.SqlsssionUtils;
import com.lagou.util.TransactionManager;
import org.apache.ibatis.binding.MapperProxyFactory;
import org.junit.Test;

/**
 * @Authther: mrwu
 * @date:2021/3/12-03
 * @Description:com.lagou.service
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
//    private Config config = new Config();
//      private AccountDao accountMapper;

    //    public AccountServiceImpl() {
//    }
    @Autowired
    private AccountDao accountDao;
//    private AccountDao accountMapper = (AccountDao)BeanFactory.getBean("accountDao");//这种方法必须先创建accountDao
//    private AccountMapper accountMapper = config.getSqlSession().getMapper(AccountMapper.class);


    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromNo, String toNo, int money) throws Exception {
        Account from = accountDao.selectObjByCardNo(fromNo);
        Account to = accountDao.selectObjByCardNo(toNo);
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        accountDao.updateAccountBycardNo(from);
//        int c = 1 / 0;
        accountDao.updateAccountBycardNo(to);
/*
        try {
            Account from = accountMapper.selectObjByCardNo(fromNo);
            Account to = accountMapper.selectObjByCardNo(toNo);
            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            accountMapper.updateAccountBycardNo(from);
            int c=1/0;
            accountMapper.updateAccountBycardNo(to);
            //提交事务
//            SqlsssionUtils.getInstance().getCurrentSqlsssion().commit();
            TransactionManager.getTransactionManager().commit();
        }catch (Exception e){
            e.printStackTrace();
            //回滚事务
//            SqlsssionUtils.getInstance().getCurrentSqlsssion().rollback();
            TransactionManager.getTransactionManager().rollback();
        }



    }

 */
    }




}